import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import Index from '../components/Index.vue';
import Shop from "../components/Shop.vue";
import SingleProduct from "../components/SingleProduct.vue"
import Cart from "../components/Cart.vue";
import About from "../components/About.vue";
import Blog from "../components/Blog.vue";
import Contact from "../components/Contact.vue";
import Account from "../components/Account.vue";
import Register from "../components/Register.vue";
import Login from "../components/Login.vue";
import Order from "../components/Order.vue";
import Dashboard from '../views/Dashboard.vue' // 控制面板
import CompanyManagement from '../views/CompanyManagement.vue' // 厂商管理
import UserManagement from '../views/UserManagement.vue' // 用户管理
import TransactionRecords from '../views/TransactionRecords.vue' // 交易记录
import SystemSettings from '../views/SystemSettings.vue' // 系统设置
import SystemMaintenance from '../views/SystemMaintenance.vue' // 系统维护
import Layout from "../components/layout.vue";
import TestRoute from "../components/TestRoute.vue";

const routes: Array<RouteRecordRaw> = [
    // 首页
    {
        path: '/',
        name: 'Index',
        component: Index,
    },
    {
        path: '/index',
        name: 'IndexAlias',
        component: Index,
    },
    
    // 公共页面 - 不使用Layout
    {
        path: '/shop',
        name: 'Shop',
        component: Shop,
    },
    {
        path: '/SingleProduct',
        name: 'SingleProduct',
        component: SingleProduct,
    },
    {
        path: '/cart',
        name: 'Cart',
        component: Cart,
    },
    {
        path: '/about',
        name: 'About',
        component: About,
    },
    {
        path: '/blog',
        name: 'Blog',
        component: Blog,
    },
    {
        path: '/contact',
        name: 'Contact',
        component: Contact,
    },
    {
        path: '/account',
        name: 'Account',
        component: Account,
    },
    {
        path: '/login',
        name: 'Login',
        component: Login,
        meta: { title: '登录' }
    },
    {
        path: '/register',
        name: 'Register',
        component: Register,
        meta: { title: '注册' }
    },
    {
        path: '/order',
        name: 'Order',
        component: Order,
    },
    {
        path: '/test',
        name: 'TestRoute',
        component: TestRoute,
    },

    // 管理页面 - 使用Layout包裹
    {
        path: '/admin',
        component: Layout,
        children: [
            {
                path: 'dashboard',
                name: 'Dashboard',
                component: Dashboard,
                meta: { title: '控制面板' }
            },
            {
                path: 'CompanyManagement',
                name: 'CompanyManagement',
                component: CompanyManagement,
                meta: { title: '厂商管理' }
            },
            {
                path: 'UserManagement',
                name: 'UserManagement',
                component: UserManagement,
                meta: { title: '用户管理' }
            },
            {
                path: 'TransactionRecords',
                name: 'TransactionRecords',
                component: TransactionRecords,
                meta: { title: '交易记录' }
            },
            {
                path: 'systemSettings',
                name: 'SystemSettings',
                component: SystemSettings,
                meta: { title: '系统设置' }
            },
            {
                path: 'systemMaintenance',
                name: 'SystemMaintenance',
                component: SystemMaintenance,
                meta: { title: '系统维护' }
            }
        ]
    },
    
    // 404页面处理
    {
        path: '/:pathMatch(.*)*',
        redirect: '/'
    }
];

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes,
    scrollBehavior(_to, _from, savedPosition) {
        // 页面切换时滚动到顶部
        if (savedPosition) {
            return savedPosition;
        } else {
            return { top: 0 };
        }
    }
})

// 移除路由守卫相关代码

export default router